﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slock.Components;
using Microsoft.Xna.Framework.Graphics;
using Slock.GameObjects;
using Microsoft.Xna.Framework;

namespace Slock.Rendering
{
    /// <summary>
    /// Uses a static vertex and index buffer to render static objects, 
    /// VERY fast yet does not allow for any kind of dynamic objects whatsoever
    /// </summary>
    public sealed class StaticVertexBufferRenderer : IRenderer
    {
        World world;
        VertexBuffer cubesInWorld;
        IndexBuffer cubeIndices;

        /// <summary>
        /// Initializes a new instance of StaticVertexBufferRenderer
        /// </summary>
        /// <param name="world"></param>
        /// <param name="buffer"></param>
        /// <param name="indices"></param>
        public StaticVertexBufferRenderer(World world,ref VertexBuffer buffer,ref IndexBuffer indices)
        {
            this.world = world;

            this.cubesInWorld = buffer;
            this.cubeIndices = indices;
        }

        /// <summary>
        /// Disposes of unmanaged content
        /// </summary>
        public void Dispose()
        {
            if (cubesInWorld != null)
                cubesInWorld.Dispose();
            if (cubeIndices != null)
                cubeIndices.Dispose();
        }

        /// <summary>
        /// Draws the buffers
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="shader"></param>
        /// <param name="camera"></param>
        public void Draw(float gameTime, Matrix view, Matrix projection)
        {
            GraphicsDevice.SetVertexBuffer(cubesInWorld);
            GraphicsDevice.Indices = cubeIndices;

            GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, cubesInWorld.VertexCount, 0, cubeIndices.IndexCount / 3);

        }

        /// <summary>
        /// Get the graphicsdevice
        /// </summary>
        public Microsoft.Xna.Framework.Graphics.GraphicsDevice GraphicsDevice
        {
            get { return world.GraphicsDevice; }
        }
    }
}
